package com.fowo.api.syncLingxingApi.service.syncImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fowo.api.syncLingxingApi.entity.TmpSyncLingxingToWarehouse;
import com.fowo.api.entity.Warehouse;
import com.fowo.api.syncLingxingApi.service.SyncLxApiBaseService;
import com.fowo.api.syncLingxingApi.service.impl.TmpSyncLingxingToWarehouseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@DS("ds24")
public class SyncLxWarehouseServiceImpl
        extends SyncLxApiBaseService<TmpSyncLingxingToWarehouse, Warehouse>  {
    private final static int MAX_LEN = 1000;
    private final static String lxApiUrlKey = "warehouseUrl";

    @Resource
    private TmpSyncLingxingToWarehouseServiceImpl tmpSyncLingxingToWarehouseService;

    public SyncLxWarehouseServiceImpl() {
        super(lxApiUrlKey, MAX_LEN);
    }

    @Override
    protected void beforeLoop() {
        super.beforeLoop();
        tmpSyncLingxingToWarehouseService.clearAll();
    }

    @Override
    protected void afterLoop() {
        super.afterLoop();
    }

    @Override
    protected JSONObject pullLingxingData(long offset, long length, JSONObject params) {
        Map<String, Object> reqBody = new HashMap<>();
        reqBody.put("offset", offset);
        reqBody.put("length", length);
        if (params != null) {
            if (params.containsKey("type")) reqBody.put("type", params.getString("type"));
            if (params.containsKey("sub_type")) reqBody.put("sub_type", params.getString("sub_type"));
            if (params.containsKey("is_delete")) reqBody.put("is_delete", params.getString("is_delete"));
        }
        return tryRequestLingxingApi(reqBody);
    }

    @Override
    protected boolean loopCond() {
        return super.loopCond();
    }

    @Override
    protected void continueDo(long totalNum, long offset, long reqTotalNum) {
        super.continueDo(totalNum, offset, reqTotalNum);
    }

    @Override
    protected boolean cmpNewAndOldEqualCond(TmpSyncLingxingToWarehouse newOne, Warehouse oldOne) {
        if (newOne == null || oldOne == null)
            return false;

//        return newOne.getWid().equals(oldOne.getWid());
        return true;
    }

    @Override
    protected TmpSyncLingxingToWarehouse cmpNewAndOldInsertProc(TmpSyncLingxingToWarehouse newOne) {
        newOne.set_flag(2);
        newOne.set_version(this.data_version);
        return newOne;
    }

    @Override
    protected TmpSyncLingxingToWarehouse cmpNewAndOldUpdateProc(TmpSyncLingxingToWarehouse newOne, Warehouse oldOne) {
        newOne.set_flag(1);
        newOne.set_version(this.data_version);
        return newOne;
    }

    @Override
    protected boolean cmpNewAndOldUpdateCond(TmpSyncLingxingToWarehouse newOne, Warehouse oldOne) {
        if (newOne == null || oldOne == null)
            return false;

        return true;
    }

    @Override
    protected void saveTempToFormal() {
        tmpSyncLingxingToWarehouseService.updateTo(this.data_version);
        tmpSyncLingxingToWarehouseService.insertInto(this.data_version);
    }

    @Override
    protected List<TmpSyncLingxingToWarehouse> jsonDumpsResult(JSONArray jsonArray) {
        List<TmpSyncLingxingToWarehouse> tmpSyncLingxingToWarehouseList = new ArrayList<>();

        for (Object obj: jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            TmpSyncLingxingToWarehouse newObj = new TmpSyncLingxingToWarehouse();

            newObj.setType(jsonObject.getString("type"));
            newObj.setName(jsonObject.getString("name"));
            newObj.set_version(this.data_version);
            newObj.setWid(jsonObject.getString("wid"));
            newObj.setIsDelete(jsonObject.getInteger("is_delete"));


            tmpSyncLingxingToWarehouseList.add(newObj);
        }

        return tmpSyncLingxingToWarehouseList;
    }

    @Override
    protected List<Warehouse> getOldData(List<TmpSyncLingxingToWarehouse> newObjects) {
        return tmpSyncLingxingToWarehouseService.getWarehouseByWid(newObjects);
    }

    @Override
    protected void saveData2TmpTb(List<TmpSyncLingxingToWarehouse> results) {
        tmpSyncLingxingToWarehouseService.saveBatch(results);
    }
}
